<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>{{title}}</title>
    <script src = "consolewrapper.js"></script>
    <!-- Load custom style sheet -->
    <link rel="stylesheet" type="text/css" href="theme/love.css">
  </head>
  <body>
    <center>
      <div>
        <canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
        <canvas id="loadingCanvas" oncontextmenu="event.preventDefault()"
                width="{{width}}" height="{{height}}"></canvas>
      </div>
    </center>

    <script type='text/javascript'>
      function goFullScreen(){
          var canvas = document.getElementById("canvas");
          if(canvas.requestFullScreen)
              canvas.requestFullScreen();
          else if(canvas.webkitRequestFullScreen)
              canvas.webkitRequestFullScreen();
          else if(canvas.mozRequestFullScreen)
              canvas.mozRequestFullScreen();
      }

      function closeFullScreen() {
          if (document.exitFullscreen) {
              document.exitFullscreen();
          } else if (document.webkitExitFullscreen) { /* Safari */
              document.webkitExitFullscreen();
          } else if (document.msExitFullscreen) { /* IE11 */
              document.msExitFullscreen();
          }
      }

      function toggleFullScreen(){
          if((window.fullScreen) || /* firefox */
             (window.innerWidth == screen.width &&  /* everything else */
              window.innerHeight == screen.height)) {
              closeFullScreen();
          } else {
              goFullScreen();
          }
      }
     var loadingContext = document.getElementById('loadingCanvas').getContext('2d');
     var complete = false;
     function drawLoadingText(text, soFar, total) {
          var canvas = loadingContext.canvas;
          var ratio = complete ? 1 : 0;
          if (soFar && total){
              ratio = soFar / total
          }
         if (ratio == 1){
             complete = true
         }
          //
          loadingContext.fillStyle = "rgb({{canvas-colour}})";
          loadingContext.fillRect(0, 0, canvas.scrollWidth, canvas.scrollHeight);

          loadingContext.font = '2em arial';
          loadingContext.textAlign = 'center'
          //
          loadingContext.fillStyle = "rgb({{text-colour}})";
          loadingContext.fillText(text, canvas.scrollWidth / 2, (canvas.scrollHeight / 2) - 40);
          //
          loadingContext.beginPath();
          loadingContext.strokeStyle = "rgb({{text-colour}})";
          loadingContext.rect((canvas.scrollWidth / 2) - 200,
                              ( canvas.scrollHeight / 2) - 20,
                              400,
                              40
              
              
          );
          loadingContext.stroke();
          loadingContext.beginPath();
          loadingContext.rect((canvas.scrollWidth / 2) - 200,
                              ( canvas.scrollHeight / 2) - 20,
                              400 * ratio,
                              40
              
              
          );
          loadingContext.fill();
          loadingContext.font = '4em arial';
          loadingContext.fillText("{{title}}", canvas.scrollWidth / 2, canvas.scrollHeight / 4);
          loadingContext.font = '2em arial';
          loadingContext.fillText("{{version}}", canvas.scrollWidth / 2, (canvas.scrollHeight / 4 + 45));
          loadingContext.font = '1em arial';
          loadingContext.textAlign = 'left'
          loadingContext.fillText("Powered By LÖVE", canvas.scrollWidth / 2 - 300, canvas.scrollHeight / 4 * 3);
         loadingContext.textAlign = 'right'
         loadingContext.fillText("Game By: {{author}}", canvas.scrollWidth / 2 + 300, canvas.scrollHeight / 4 * 3);
     }

     window.onload = function () { window.focus(); };
      window.onclick = function () { window.focus(); };

      window.addEventListener("keydown", function(e) {
        // space and arrow keys
        if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
          e.preventDefault();
        }
      }, false);

      var Module = {
          arguments: ["game.love","web"],
        INITIAL_MEMORY: {{initial-memory}},
        printErr: console.error.bind(console),
        canvas: (function() {
          var canvas = document.getElementById('canvas');

          // As a default initial behavior, pop up an alert when webgl context is lost. To make your
          // application robust, you may want to override this behavior before shipping!
          // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
          canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);

          return canvas;
        })(),
        setStatus: function(text, soFar, total) {
          if (text) {
              drawLoadingText(text, soFar, total);
          } else if (Module.remainingDependencies === 0) {
            document.getElementById('loadingCanvas').style.display = 'none';
            document.getElementById('canvas').style.display = 'block';
          }
        },
        totalDependencies: 0,
        remainingDependencies: 0,
        monitorRunDependencies: function(left) {
          this.remainingDependencies = left;
          this.totalDependencies = Math.max(this.totalDependencies, left);
          Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.',
                           this.totalDependencies-left,
                           this.totalDependencies);
        }
      };
      Module.setStatus('Downloading...');
      window.onerror = function(event) {
        // TODO: do not warn on ok events like simulating an infinite loop or exitStatus
        Module.setStatus('Exception thrown, see JavaScript console');
        Module.setStatus = function(text) {
          if (text) Module.printErr('[post-exception status] ' + text);
        };
      };

      var applicationLoad = function(e) {
        Love(Module);
      }
    </script>
    <script type="text/javascript" src="game.js"></script>
    <script async type="text/javascript" src="love.js" onload="applicationLoad(this)"></script>
    <!-- <footer> -->
    <!--   <button onclick="goFullScreen();">Go Fullscreen</button> -->
    <!-- </footer> -->
  </body>
</html>
